package vsm.vsm_server.mapper;

import org.apache.ibatis.annotations.*;
import vsm.vsm_server.pojo.User.Record.History.UserVideoHistoryDTO;
import vsm.vsm_server.pojo.User.Record.Like.LikeVideo;
import vsm.vsm_server.pojo.User.Record.Like.UserVideoIsLikeDTO;
import vsm.vsm_server.pojo.Video.Video;

import java.math.BigInteger;
import java.util.List;
import java.util.Set;

@Mapper
public interface LikeMapper {
    /*
    查询用户喜欢列表
     */
    @Select("select * from videolikes where userid = #{userid}")
    List<LikeVideo> getLikeVideos(BigInteger userId);
    /*
    查询用户是否喜欢该视频
     */
    @Select("select exists(select * from videolikes where userid = #{userId} and videoid = #{videoId}) ")
    Boolean isLikeVideo(LikeVideo likeVideo);

    /*
    用户喜欢该视频
     */
    @Insert("insert into videolikes (userid,videoid) values (#{userId},#{videoId})")
    void likeVideo(LikeVideo likeVideo);
    /*
    用户取消 喜欢该视频
     */
    @Delete("delete from videolikes where userid=#{userId} and videoid=#{videoId}")
    void unlikeVideo(LikeVideo likeVideo);


    /**
     * 查询给定的 (userId, videoId) 对列表中，哪些组合在 videolikes 表中存在。
     * @return 存在于 videolikes 表中的 (userId, videoId) 对的 Set
     */
    @Select("""
            <script>
            SELECT userId, videoId
            FROM videolikes
            WHERE (userId, videoId) IN
            <foreach collection='pairs' item='pair' open='(' separator=',' close=')'>
              (#{pair.userId}, #{pair.videoId})
            </foreach>
            </script>
            """)
    // 映射到 VideoLikeSimpleDto
    @Results({
            @Result(property = "userId", column = "userId"),
            @Result(property = "videoId", column = "videoId")
    })
    List<UserVideoIsLikeDTO> isLikeVideoList(@Param("pairs") List<UserVideoHistoryDTO> results);

    /**
     * 查询给定的 (userId, videoId) 对列表中，哪些组合在 videolikes 表中存在。
     * @param pairs DTO 列表，每个 DTO 包含 userId 和 videoId
     * @return 存在于 videolikes 表中的 (userId, videoId) 对的 Set
     */
    @Select("""
            <script>
            SELECT userId, videoId
            FROM videolikes
            WHERE (userId, videoId) IN
            <foreach collection='pairs' item='pair' open='(' separator=',' close=')'>
              (#{pair.userId}, #{pair.videoId})
            </foreach>
            </script>
            """)
    // --- *** 修改返回类型为 Set<VideoLikeSimpleDto> *** ---
    @Results({
            @Result(property = "userId", column = "userId"),
            @Result(property = "videoId", column = "videoId")
    })
    Set<LikeVideo> findExistingLikesInBatch(@Param("pairs") List<UserVideoHistoryDTO> pairs);

}

